/**
 * 
 */
package org.xlab.baseframework.web.component;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.vaadin.peter.buttongroup.ButtonGroup;
import org.xlab.baseframework.domain.ISystemUserDomain;
import org.xlab.baseframework.domain.SystemParameter;
import org.xlab.baseframework.domain.view.SystemParameterVO;
import org.xlab.baseframework.exception.PermissionDeniedException;
import org.xlab.baseframework.exception.SystemParameterNotFoundException;
import org.xlab.baseframework.exception.SystemRuntimeException;
import org.xlab.baseframework.service.api.ISystemParameterService;

import com.vaadin.data.Item;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Form;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * @author makubex
 * 
 */
public class ParameterEditWindow extends Window {

	/**
	 * @serialField
	 */
	private static final long serialVersionUID = 6218161880401813359L;
	private Form editForm;
	private Button save;
	private Item editItem;
	private Button reset;

	private ButtonGroup buttons;

	@Autowired(required=true)
	@Qualifier("bpf_systemParameterService")
	private ISystemParameterService systemParameterService;

	/**
	 * @param caption
	 */
	public ParameterEditWindow(String caption) {
		super(caption);
		super.setModal(true);
	}

	/**
	 * @see com.vaadin.ui.Panel#attach()
	 */
	public void attach() {
		super.attach();
		VerticalLayout mainLayout = new VerticalLayout();
		mainLayout.setMargin(false);
		mainLayout.setSpacing(true);
		mainLayout.setSizeFull();
		this.editForm = new Form();
		this.editForm.setImmediate(true);
		this.editForm.setItemDataSource(this.editItem);
		this.editForm.setSizeFull();
		this.buttons = new ButtonGroup();
		this.save = new Button("Save");
		this.save.addListener(new ClickListener() {

			/**
			 * @serialField
			 */
			private static final long serialVersionUID = 5291543744925218400L;

			public void buttonClick(ClickEvent event) {
				SystemParameterVO parameterVO = (SystemParameterVO) editForm.getValue();
				SystemParameter parameterInstance = systemParameterService.parseFromVO(parameterVO);
				try {
					systemParameterService.updateNonSystemParameter(parameterInstance, 
							(ISystemUserDomain) ParameterEditWindow.super
									.getApplication().getUser());
				} catch (UnsupportedOperationException e) {
					e.printStackTrace();
				} catch (SystemRuntimeException e) {
					e.printStackTrace();
				} catch (PermissionDeniedException e) {
					e.printStackTrace();
				} catch (SystemParameterNotFoundException e) {
					e.printStackTrace();
				}
			}
		});
		this.reset = new Button("Reset");
		this.reset.addListener(new ClickListener() {
			
			/**
			 * @serialField
			 */
			private static final long serialVersionUID = -3131221000775448675L;

			public void buttonClick(ClickEvent event) {
				editForm.discard();
			}
		});
		this.buttons.addButton(save);
		this.buttons.addButton(reset);
		mainLayout.addComponent(this.editForm);
		mainLayout.addComponent(this.buttons);
		mainLayout.setComponentAlignment(this.editForm, Alignment.TOP_LEFT);
		mainLayout.setComponentAlignment(this.buttons, Alignment.MIDDLE_CENTER);
		super.addComponent(mainLayout);
	}

	/**
	 * @return the editItem
	 */
	public Item getEditItem() {
		return editItem;
	}

	/**
	 * @param editItem
	 *            the editItem to set
	 */
	public void setEditItem(Item editItem) {
		this.editItem = editItem;
	}

	/**
	 * @param systemParameterService the systemParameterService to set
	 */
	public void setSystemParameterService(
			ISystemParameterService systemParameterService) {
		this.systemParameterService = systemParameterService;
	}


}
